<template>
	<view class="" style="width: 100%;">

		<button @click="show = true" :class="showImg ? 'checkedUplpad' : 'img_view1'" style="margin-right: 10rpx;" v-show="srcList.length < num">
			<u-icon name="plus" size="40"></u-icon>
		</button>
		<view style="display: inline-block;" v-for="(item, index) of srcList" :key="item">
			<view class="img_view">
				<u-icon name="close-circle-fill" color="red" @click="clearimg(item, index)" size="28"
					style="position: absolute; z-index: 33; top: 0%; right: 0%;  transform: translate(26%, -26%)"></u-icon>
				<image :showLoading="true" :src="item" mode="" style="width: 100%; height: 100%; border-radius: 10%;"
					@click="sebigImg(item)">
				</image>
			</view>
			<view v-if="showImg" style="margin-top: 20rpx;">
				<u-radio-group v-model="radiovalue1" placement="column">
					<u-radio :customStyle="{marginBottom: '8px'}" :key="index" label="设置主图" :name="item"
						@change="radioChange">
					</u-radio>
				</u-radio-group>
			</view>
		</view>
		<view
			style=" border-radius: 10rpx; margin-right: 10rpx;  float: left; position: relative;width: 160rpx; height: 160rpx;"
			v-for="(item, index) of videoList" :key="item" @click.stop="seeVideo(item)">
			<!-- close-circle -->
			<u-icon name="close-circle-fill" @click="clearVide(item, index)" size="28" color="red"
				style="position: absolute; z-index: 33; top: -10%; right: -10%; transform: translate(26%, -26%)"></u-icon>

			<video :controls="false" :src="item" style="width: 100%;height: 100%;" @play="seeVideo(item)"></video>
		</view>

		<u-overlay :show="videoIsShow" @click="videoIsShow = false">
			<view class=""
				style="height: 100vh; width: 100vw;display: flex; justify-content: center; align-content: center;">
				<video :src="videoSrc" style="width: 100%; margin: auto;" :opacity="0.7"></video>
			</view>
		</u-overlay>
		<u-popup :show="show" @close="show = false" @open="show = true" style="width: 100%;">
			<view class="" style="width: 100%; padding: 50rpx 0; border-radius: 20rpx 20rpx; height: auto;">
				<u-upload @afterRead="afterRead"  name="1" multiple :maxCount="10"
					style="width: 100%;" v-if="type == 'img' || type =='all'">
					<u-cell-group style="width: 100vw">
						<u-cell style="width: 100%;">
							<template slot="title">
								<view style="width: 100%;">选择图片</view>
							</template>
							<u-icon name="arrow-right" slot="right-icon"></u-icon>
						</u-cell>
					</u-cell-group>
				</u-upload>
			
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		baseURL
	} from "@/request/index.js"

	export default {
		data() {
			return {
				checked: '',
				mainImageIndex: -1, // 记录当前主图的索引，默认没有主图
				show: false,
				// fileList: [],
				videoSrc: "",
				videoIsShow: false,
				videoList: [],
				srcList: [],
				radiovalue1: ""
			}
		},
		props: {
			type: {
				type: String,
				default: "all"
			},
			num: {
				type: [Number, String],
				default: 10,
			},
			showImg: {
				type: Boolean,
				default: false
			},
			fileList: {
				type: Array,
				default:() => []
			}
		}, 
		mounted() {
			setTimeout(() => {
				this.srcList.push(...this.fileList)
			}, 500)
		},
		methods: {
			radioChange(n) {
				this.$emit('imgchecked', n)
			},
			//删除
			clearimg(srcitem) {
				this.srcList = this.srcList.filter((item, index) => {
					return srcitem != item
				})
 
				this.$emit("change", this.srcList)
			 
			},
	
   
			sebigImg(src) {
				uni.previewImage({
					current: src,
					urls: this.srcList,
				})
			},
		
			// 删除图片
			// deletePic(event) {
			// 	this[`fileList${event.name}`].splice(event.index, 1)
			// },
			// 新增图片
			async afterRead(event) {
				if (event.file[0].size > 200000000) {
					return uni.showToast({
						title: '不得超过200MB',
						icon: 'none'
					})
				}
				let lists = [].concat(event.file)
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					if (result.code != 200) {
						uni.showToast({
							title: '上传失败',
							icon: 'error'
						})
					} else {
						// if (lists[i].type != "image") {
						// 	this.videoList.push(result.data)
						// } else {
						// 	this.fileList.push(result.data)
						// }
						// this.srcList.push(result.data) 
						this.srcList.push(result.data)
					 
						
					}
					this.show = false

				}
				this.$emit("change", this.srcList )
			},

			uploadFilePromise(url) {
				let loadding = uni.showLoading({
					title: '上传中',
					mask: true
				})
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseURL + '/face/fileup',
						filePath: url,
						name: 'file',
						success: (res) => {
							resolve(JSON.parse(res.data))
							uni.hideLoading(loadding)
						},
						fail: () => {
							uni.hideLoading(loadding)
						}
					});
				})
			},
		}

	}
</script>

<style lang="scss" scoped>
	.img_view {
		width: 160rpx;
		height: 160rpx;
		position: relative;
	}

	.img_view1 {
		display: inline-block;
		width: 160rpx;
		height: 160rpx;
		text-align: center;
		padding: 40rpx;
		box-sizing: border-box;
	}
	.checkedUplpad {
		display: inline-block;
		width: 200rpx;
		height: 200rpx;
		text-align: center;
		padding: 60rpx;
		box-sizing: border-box;
	}
	::v-deep .u-upload__wrap {
		width: 100% !important;
	}

	::v-deep .u-cell__body {
		width: 100% !important;
	}
</style>